﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using XQ.SP.Model;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using HelpWeb.WeiXin;

namespace System
{
    public class PM
    {
        public static bool IsMobile()
        {
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                HttpRequest request = context.Request;
                if (request.Browser.IsMobileDevice)
                {
                    string tStr = request.UserAgent.ToString();
                    //WriteRecord(tStr);
                    return true;
                }

                //string MobileUserAgent = System.Configuration.ConfigurationManager.AppSettings["MobileUserAgent"];
                //string MobileUserAgent = "iphone|android|nokia|zte|huawei|lenovo|samsung|motorola|sonyericsson|lg|philips|gionee|htc|coolpad|symbian|sony|ericsson|mot|cmcc|iemobile|sgh|panasonic|alcatel|cldc|midp|wap|mobile|blackberry|windows ce|mqqbrowser|ucweb";
                string MobileUserAgent = "iphone|android|nokia|zte|huawei|lenovo|samsung|motorola|sonyericsson|lg|philips|gionee|htc|coolpad|symbian|sony|ericsson|mot|cmcc|iemobile|sgh|panasonic|alcatel|cldc|midp|wap|mobile|blackberry|windows ce|mqqbrowser|ucweb|mqq|mobile";
                Regex MOBILE_REGEX = new Regex(MobileUserAgent);

                if (string.IsNullOrEmpty(request.UserAgent) || MOBILE_REGEX.IsMatch(request.UserAgent.ToLower()))
                {
                    string tStr = request.UserAgent.ToString();
                    //WriteRecord(tStr);
                    return true;
                }
                string tStr1 = request.UserAgent.ToString();
                //WriteRecord(tStr1);
            }
            return false;
        }


        /// <summary>
        /// 发送微信信息（通知类到我的微信号上
        /// </summary>
        /// <param name="model"></param>
        public void SendWinXinSelf(string sendStr)
        {
            #region
            HelpWeb.WeiXin.WeiXinHelper wxHper = new WeiXinHelper();
            string tokenStr = wxHper.Get_tokenOnly();
            string strJson = @"{""industry_id1"": ""1"", ""industry_id2"": ""10""}";
            string wxurl = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=" + tokenStr;
            WebClient myWebClient = new WebClient();
            myWebClient.Credentials = CredentialCache.DefaultCredentials;

            string result = "";
            string templeId = "";

            //发送模板信息
            string mulStr1 = @"""first"":{""value"":""新定单提示"",""color"":""#173177""}";
            string mulStr2 = @"""keynote1"":{""value"":""方便面"",""color"":""#173177""}";
            string mulStr3 = @"""keynote2"":{""value"":""20块"",""color"":""#173177""}";
            string mulStr4 = @"""keynote3"":{""value"":""2014年9月16日"",""color"":""#173177""}";
            string mulStr5 = @"""remark"":{""value"":""请即时配送！"",""color"":""#173177""}";
            string mulStr = @"""data"":{" + mulStr1 + "," + mulStr2 + "," + mulStr3 + "," + mulStr4 + "," + mulStr5 + "}";

            //var intXiaoQu = UsersInfo.GetXiaoQuID();
            //XQ.BLL.Base.BaseXiaoQu bllxq = new XQ.BLL.Base.BaseXiaoQu();
            //var itemxu = bllxq.GetModel(intXiaoQu);

            //XQ.SP.BllServices.StoreBaseUserBLL bllsbu = new StoreBaseUserBLL();
            //var itemsbu = bllsbu.GetModelList(" StoreBaseID=" + itemxu.WebUrl);

            //XQ.BLL.Store.StoreBase bllsb = new XQ.BLL.Store.StoreBase();
            //var itemSb = bllsb.GetModel(int.Parse(itemxu.WebUrl));

            //var itemInfo = UsersInfo.GetusersInfo();
            //if (!string.IsNullOrEmpty(itemInfo.SaleUserFile))
            //{
            //    #region 发送 通知
            //    string m11 = @"""name"":{""value"":""系统通知信息"",""color"":""#173177""}";
            //    string m55 = @"""remark"":{""value"":""" + sendStr + @",请即时处理." + @""",""color"":""#173177""}";
            //    templeId = "NK1JFyy6CmVtoIR_VC-77wP7ow6GKXyX1lHlE4MgVX4";
            //    mulStr = @"""data"":{" + m11 + "," + m55 + "}";
            //    strJson = @"{""touser"": """ + itemInfo.SaleUserFile + @""", ""template_id"": """ + templeId + @""", ""url"": ""http://weixin.58xiao.com/" + @""", ""topcolor"": ""#FF0000"", " + mulStr + "}";
            //    wxurl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + tokenStr;
            //    try
            //    {
            //        myWebClient = new WebClient();
            //        myWebClient.Credentials = CredentialCache.DefaultCredentials;
            //        myWebClient.Encoding = UnicodeEncoding.UTF8;
            //        result = myWebClient.UploadString(wxurl, "POST", strJson);
            //    }
            //    catch (Exception ex)
            //    {
            //        result = "发送模板Error:1" + ex.Message;
            //        HandleException(result);
            //    }

            //    #endregion
            //}

            string m1 = @"""first"":{""value"":""系统通知"",""color"":""pink""}";
            string m2 = @"""Day"":{""value"":""" + DateTime.Now + @" "",""color"":""pink""}";
            string m3 = @"""orderId"":{""value"":""" + @""",""color"":""#173177""}";
            string m7 = @"""orderType"":{""value"":""即时处理"",""color"":""#173177""}";
            string m4 = @"""customerName"":{""value"":""小马哥" + @""",""color"":""#173177""}";
            string m6 = @"""customerPhone"":{""value"":""没有" + @""",""color"":""#173177""}";
            string m5 = @"""remark"":{""value"":""" + sendStr + @"。"",""color"":""#173177""}";
            //string m5 = @"""remark"":{""value"":""" + model.UsersAddress + @""",""color"":""#173177""}";
            templeId = "1hss0Mylj8eVGD3K8JyiPbvp2cl_XAg050J06LjkPn0";
            mulStr = @"""data"":{" + m1 + "," + m2 + "," + m3 + "," + m7 + "," + m4 + "," + m5 + "," + m6 + "}";

            #region 发送到我的微信 新订单
            strJson = @"{""touser"": ""oPtLowifXQLg4KwV5mQAg_AJ2aFk"", ""template_id"": """ + templeId + @""", ""url"": ""http://weixin.58xiao.com/SPStore/MySellWonly?noStr=" + @""", ""topcolor"": ""#FF0000"", " + mulStr + "}";
            wxurl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + tokenStr;
            try
            {
                myWebClient = new WebClient();
                myWebClient.Credentials = CredentialCache.DefaultCredentials;
                myWebClient.Encoding = UnicodeEncoding.UTF8;
                result = myWebClient.UploadString(wxurl, "POST", strJson);
            }
            catch (Exception ex)
            {
                result = "发送模板Error:1" + ex.Message;
            }
            #endregion

            #endregion
        }

        //public static void WriteRecord(string tempStr)
        //{
        //    //string pathStr = System.Environment.CurrentDirectory;
        //    string pathStr = HttpRuntime.AppDomainAppPath.ToString();
        //    pathStr += "\\LogWin\\";
        //    if (!System.IO.Directory.Exists(pathStr))
        //    {
        //        System.IO.Directory.CreateDirectory(pathStr);
        //    }
        //    DateTime now = DateTime.Now;
        //    string logpath = string.Format(@"Error_Record.log", now.Year, now.Month, now.Day, now.Hour);
        //    logpath = pathStr + logpath;

        //    FileInfo finfo = new FileInfo(logpath);
        //    if (!finfo.Exists)
        //    {
        //        FileStream fs;
        //        fs = File.Create(logpath);
        //        fs.Close();
        //        finfo = new FileInfo(logpath);
        //    }
        //    using (FileStream fs = finfo.OpenWrite())
        //    {
        //        /**/
        //        ///根据上面创建的文件流创建写数据流
        //        StreamWriter w = new StreamWriter(fs);
        //        /**/
        //        ///设置写数据流的起始位置为文件流的末尾
        //        w.BaseStream.Seek(0, SeekOrigin.End);
        //        w.Write(tempStr + "\r\n\r\n");
        //        /**/
        //        ///清空缓冲区内容，并把缓冲区内容写入基础流
        //        w.Flush();
        //        /**/
        //        ///关闭写数据流
        //        w.Close();
        //    }
        //}
    }
}
